package com.gk.carshopsys.controller;

import com.gk.carshopsys.service.CustService;
import com.gk.carshopsys.service.EmpService;
import com.gk.carshopsys.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.lang.reflect.Array;
import java.util.*;

@Controller
public class MainController {
    @Autowired
    MainService mainService;

    @RequestMapping("/sysintroduction")
    public String tosyi(){
        return "sysintroduction";
    }

    @RequestMapping("/main")
    public String toMain(Model model){
        //数据统计
        Long empcount = mainService.findEmpCount();
        Long custcount = mainService.findCustCount();
        Long ordercount = mainService.findOrderCount();
        Long incomecount = mainService.findincomeCount();

        model.addAttribute("empcount",empcount);
        model.addAttribute("custcount",custcount);
        model.addAttribute("ordercount",ordercount);
        model.addAttribute("incomecount",incomecount);

        //地区分布饼图
        List<Map> address = mainService.findAddressNum();

        List<String> keylist = new ArrayList<>();
        List<Integer> valuelist = new ArrayList<>();

        for (Map m : address){
            Map mp ;
            mp = m;
            valuelist.add(Integer.valueOf(mp.get("addressnum").toString()));
            keylist.add("\""+mp.get("address")+"\"");
        }
        model.addAttribute("address",keylist);
        model.addAttribute("addressnum",valuelist);


        //销量图
        List<Map> sale = mainService.findCarSales();

        List<String> carlist = new ArrayList<>();
        List<Integer> salelist = new ArrayList<>();
        for (Map m : sale){
            Map mp ;
            mp = m;
            salelist.add(Integer.valueOf(mp.get("sale").toString()));
            carlist.add("\""+mp.get("carname")+"\"");

        }
        model.addAttribute("carname",carlist);
        model.addAttribute("sale",salelist);

        return "main";
    }

    @RequestMapping("/empmain")
    public String toEmpMain(Model model){
        //数据统计
        Long empcount = mainService.findEmpCount();
        Long custcount = mainService.findCustCount();
        Long ordercount = mainService.findOrderCount();
        Long incomecount = mainService.findincomeCount();

        model.addAttribute("empcount",empcount);
        model.addAttribute("custcount",custcount);
        model.addAttribute("ordercount",ordercount);
        model.addAttribute("incomecount",incomecount);

        //地区分布饼图
        List<Map> address = mainService.findAddressNum();

        List<String> keylist = new ArrayList<>();
        List<Integer> valuelist = new ArrayList<>();

        for (Map m : address){
            Map mp ;
            mp = m;
            valuelist.add(Integer.valueOf(mp.get("addressnum").toString()));
            keylist.add("\""+mp.get("address")+"\"");
        }
        model.addAttribute("address",keylist);
        model.addAttribute("addressnum",valuelist);


        //销量图
        List<Map> sale = mainService.findCarSales();

        List<String> carlist = new ArrayList<>();
        List<Integer> salelist = new ArrayList<>();
        for (Map m : sale){
            Map mp ;
            mp = m;
            salelist.add(Integer.valueOf(mp.get("sale").toString()));
            carlist.add("\""+mp.get("carname")+"\"");

        }
        model.addAttribute("carname",carlist);
        model.addAttribute("sale",salelist);

        return "/emp/empmain";
    }


}
